<template>
  <div>
    <button @click="openCreate">新增一条记录</button>

    <table v-if="showCreate">
      <tr v-for="col in mycols">
        <td>{{ col.name }}</td>
        <td><input v-model="map[col.col]" value=""></td>
      </tr>
      <tr><td><button @click="save">保存</button></td>
        <td><button @click="closeCreate">关闭</button></td></tr>
    </table>
  </div>

</template>

<script>
  import {mapGetters} from 'vuex'

  var config = require('../../config')

  export default {
    data () {
      return {map:{}}
    },
    computed: {
      ...mapGetters({
        mycols: 'initCols',
        showCreate: 'showCreate'
      })
    },
    methods: {
      test: function (value) {
        alert(value);
      },
      openCreate: function () {
        this.$store.commit('openCreate')
      },
      closeCreate: function() {
        this.$store.commit('closeCreate')
      },
      save: function() {
        console.log(this.map)
        var self = this
        this.$axios.post(config.dev.server+'/vue/main/addStudent.json',this.map)
          .then(function (res) {

            alert('保存成功')
            self.$emit('save')
          })
      }
    },
    created () {
      this.closeCreate()
    }
  }

</script>
